package jp.co.iris_ltd.irisphotoeditor.util;

import java.util.Stack;

public class HistoryStack<T> {

    private final Stack<T> undoStack = new Stack<T>();
    private final Stack<T> redoStack = new Stack<T>();

    /**
     * Undo.
     * 
     * @return <T>
     */
    public T undo() {
        T result = null;
        if (!undoStack.empty()) {
            result = undoStack.pop();
            redoStack.push(result);
        }

        return result;
    }

    /**
     * Redo.
     * 
     * @return <T>
     */
    public T redo() {
        T result = null;
        if (!redoStack.empty()) {
            result = redoStack.pop();
            undoStack.push(result);
        }

        return result;
    }

    /**
     * Add history.
     * 
     * @param history
     */
    public void add(T history) {
        undoStack.push(history);
        redoStack.clear();
    }

    /**
     * Iterate undo.
     * 
     * @return Iterable<T>
     */
    public final Stack<T> iterateUndo() {
        return undoStack;
    }

}
